package com.sdhs.paas.itims.app;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.TypeReference;
import com.sdhs.paas.itims.bean.VersionPublishDetailBean;
import com.sdhs.paas.itims.dto.VersionPublishDetailDTO;
import com.sdhs.paas.itims.dto.req.VersionPublishDetailReqDTO;
import com.sdhs.paas.itims.dto.res.VersionPublishDetailResDTO;
import com.sdhs.paas.itims.service.VersionPublishDetailService;
import com.sdhs.paas.sdk.base.common.BasePage;
import com.sdhs.paas.sdk.base.dto.PageInfoDTO;
import com.sdhs.paas.sdk.base.page.Page;
import com.sdhs.paas.sdk.base.utils.EntityDtoConvertUtil;
import com.sdhs.paas.sdk.core.PaasBaseRequest;
import com.sdhs.paas.sdk.core.PaasBaseResponse;
import com.sdhs.paas.sdk.core.RefreshableRestController;

/**
 * @author VersionPublishDetail
 * @version 1.0
 * @Title:  代码生成接口，用于接受前端传输过来的数据
 * @author kangjichang
 * @date 2018-09-18
 */
 
@RefreshableRestController
@RequestMapping(value = "/versionPublishDetail")
public class VersionPublishDetailController {

    @Autowired
    private VersionPublishDetailService versionPublishDetailService;
    
    /**
     * 分页查询接口
     * */
    @PostMapping("/getVersionPublishDetailPageList")
	public PaasBaseResponse<VersionPublishDetailResDTO> getVersionPublishDetailPageList(@RequestBody PaasBaseRequest<VersionPublishDetailReqDTO> versionPublishDetailReqDTO) {
    	VersionPublishDetailDTO versionPublishDetailDTO = versionPublishDetailReqDTO.getBody().getVersionPublishDetailDTO();
    	PageInfoDTO pageInfoDTO = versionPublishDetailReqDTO.getBody().getPageInfo();
    	
		BasePage pageRequest = new BasePage();
		pageRequest.setPage(pageInfoDTO.getPage());
		pageRequest.setCount(pageInfoDTO.getPageSize());
    	
		VersionPublishDetailBean versionPublishDetailBean = EntityDtoConvertUtil.convertToEntity(versionPublishDetailDTO, VersionPublishDetailBean.class);
    	Page<VersionPublishDetailBean> page = versionPublishDetailService.getPageVersionPublishDetailList(pageRequest, versionPublishDetailBean);
    	PageInfoDTO resPageInfoDTO = new PageInfoDTO();
    	VersionPublishDetailResDTO versionPublishDetailResDTO = new VersionPublishDetailResDTO();
    	resPageInfoDTO.setPage(page.getCurPage());
		resPageInfoDTO.setPageSize(page.getPageSize());
		resPageInfoDTO.setPageCount(page.getPageCount());
		resPageInfoDTO.setRowCount(page.getTotalCount());
		versionPublishDetailResDTO.setPageInfo(resPageInfoDTO);
		List<VersionPublishDetailBean> versionPublishDetailBeanList = page.getResult();
		List<VersionPublishDetailDTO> dtoList = new ArrayList<VersionPublishDetailDTO>();
		dtoList = EntityDtoConvertUtil.convertToDTOList(versionPublishDetailBeanList, new TypeReference<List<VersionPublishDetailDTO>>() {
		});
		versionPublishDetailResDTO.setVersionPublishDetailDTOList(dtoList);
    	return new PaasBaseResponse<>(versionPublishDetailResDTO);
	}
	
    @PostMapping("/getVersionPublishDetailList")
   	public PaasBaseResponse<VersionPublishDetailResDTO> getVersionPublishDetailList(@RequestBody PaasBaseRequest<VersionPublishDetailReqDTO> versionPublishDetailReqDTO) {
       	VersionPublishDetailDTO versionPublishDetailDTO = versionPublishDetailReqDTO.getBody().getVersionPublishDetailDTO();
       	VersionPublishDetailBean versionPublishDetailBean = EntityDtoConvertUtil.convertToEntity(versionPublishDetailDTO, VersionPublishDetailBean.class);
       	List<VersionPublishDetailBean> versionPublishDetailBeanList = versionPublishDetailService.getList(versionPublishDetailBean);
       	List<VersionPublishDetailDTO> dtoList = new ArrayList<VersionPublishDetailDTO>();
		dtoList = EntityDtoConvertUtil.convertToDTOList(versionPublishDetailBeanList, new TypeReference<List<VersionPublishDetailDTO>>() {
		});
		VersionPublishDetailResDTO versionPublishDetailResDTO = new VersionPublishDetailResDTO();
		versionPublishDetailResDTO.setVersionPublishDetailDTOList(dtoList);
    	return new PaasBaseResponse<>(versionPublishDetailResDTO);
    }
    
	/**
	 * 保存接口
	 * */
	@PostMapping("/saveVersionPublishDetail")
	public PaasBaseResponse<VersionPublishDetailResDTO> saveVersionPublishDetail(@RequestBody PaasBaseRequest<VersionPublishDetailReqDTO> versionPublishDetailReqDTO) {
		VersionPublishDetailDTO versionPublishDetailDTO = versionPublishDetailReqDTO.getBody().getVersionPublishDetailDTO();
		VersionPublishDetailBean versionPublishDetailBean = EntityDtoConvertUtil.convertToEntity(versionPublishDetailDTO, VersionPublishDetailBean.class);
		String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
		versionPublishDetailBean.setId(uuid);
		versionPublishDetailBean.setIsDeleted(0);
		Boolean flag = versionPublishDetailService.saveVersionPublishDetail(versionPublishDetailBean);
		versionPublishDetailDTO.setId(uuid);
		VersionPublishDetailResDTO versionPublishDetailResDTO = new VersionPublishDetailResDTO();
		versionPublishDetailResDTO.setVersionPublishDetailDTO(versionPublishDetailDTO);
		return new PaasBaseResponse<>(versionPublishDetailResDTO);
	}
	
	/**
	 * 修改接口
	 * */
	@PostMapping("/updateVersionPublishDetail")
	public PaasBaseResponse<VersionPublishDetailResDTO> updateVersionPublishDetail(@RequestBody PaasBaseRequest<VersionPublishDetailReqDTO> versionPublishDetailReqDTO) {
		VersionPublishDetailDTO versionPublishDetailDTO = versionPublishDetailReqDTO.getBody().getVersionPublishDetailDTO();
		VersionPublishDetailBean versionPublishDetailBean = EntityDtoConvertUtil.convertToEntity(versionPublishDetailDTO, VersionPublishDetailBean.class);
		Boolean flag = versionPublishDetailService.updateVersionPublishDetail(versionPublishDetailBean);
		VersionPublishDetailResDTO versionPublishDetailResDTO = new VersionPublishDetailResDTO();
		versionPublishDetailResDTO.setVersionPublishDetailDTO(versionPublishDetailDTO);
		return new PaasBaseResponse<>(versionPublishDetailResDTO);
	}
	
	/**
	 * 获取详情接口
	 * */
	@PostMapping("/getVersionPublishDetailDetail")
	public PaasBaseResponse<VersionPublishDetailResDTO> getVersionPublishDetailDetail(@RequestBody PaasBaseRequest<VersionPublishDetailReqDTO> versionPublishDetailReqDTO) {
		VersionPublishDetailDTO versionPublishDetailDTO = versionPublishDetailReqDTO.getBody().getVersionPublishDetailDTO();
		VersionPublishDetailBean versionPublishDetailBean = EntityDtoConvertUtil.convertToEntity(versionPublishDetailDTO, VersionPublishDetailBean.class);
		versionPublishDetailBean = versionPublishDetailService.getVersionPublishDetailDetail(versionPublishDetailBean.getId());
		versionPublishDetailDTO = EntityDtoConvertUtil.convertToDTO(versionPublishDetailBean, VersionPublishDetailDTO.class);
		VersionPublishDetailResDTO versionPublishDetailResDTO = new VersionPublishDetailResDTO();
		versionPublishDetailResDTO.setVersionPublishDetailDTO(versionPublishDetailDTO);
		return new PaasBaseResponse<>(versionPublishDetailResDTO);
	}
	
	/**
	 * 删除接口
	 * */
	@PostMapping("/deleteVersionPublishDetail")
	public PaasBaseResponse<VersionPublishDetailResDTO> deleteVersionPublishDetail(@RequestBody PaasBaseRequest<VersionPublishDetailReqDTO> versionPublishDetailReqDTO) {
		VersionPublishDetailDTO versionPublishDetailDTO = versionPublishDetailReqDTO.getBody().getVersionPublishDetailDTO();
		VersionPublishDetailBean versionPublishDetailBean = EntityDtoConvertUtil.convertToEntity(versionPublishDetailDTO, VersionPublishDetailBean.class);
		Boolean flag = versionPublishDetailService.deleteVersionPublishDetail(versionPublishDetailBean);
		VersionPublishDetailResDTO versionPublishDetailResDTO = new VersionPublishDetailResDTO();
		return new PaasBaseResponse<>(versionPublishDetailResDTO);
	}
}
